package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.tencentcloudapi.cdn.v20180606.models.DescribeMapInfoRequest;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapProperties;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @SneakyThrows
    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        Credential credential = new Credential(vodConstantProperties.getSecretId(),vodConstantProperties.getSecretKey());
        VodClient vodClient = new VodClient(credential,vodConstantProperties.getRegion());
        DescribeMediaInfosRequest describeMediaInfosRequest = new DescribeMediaInfosRequest();
        describeMediaInfosRequest.setFileIds(new String[]{mediaFileId});
        DescribeMediaInfosResponse response = vodClient.DescribeMediaInfos(describeMediaInfosRequest);
        TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
        MediaBasicInfo basicInfo = response.getMediaInfoSet()[0].getBasicInfo();
        MediaMetaData mediaMetaData = response.getMediaInfoSet()[0].getMetaData();
        trackMediaInfoVo.setSize(mediaMetaData.getSize());
        trackMediaInfoVo.setMediaUrl(basicInfo.getMediaUrl());
        trackMediaInfoVo.setDuration(mediaMetaData.getDuration());
        trackMediaInfoVo.setType(basicInfo.getType());
        return trackMediaInfoVo;
    }

    @SneakyThrows
    @Override
    public void deleteMediaFile(String mediaFileId) {
        Credential credential = new Credential(vodConstantProperties.getSecretId(),vodConstantProperties.getSecretKey());
        VodClient vodClient = new VodClient(credential,vodConstantProperties.getRegion());
        DeleteMediaRequest request = new DeleteMediaRequest();
        request.setFileId(mediaFileId);
        DeleteMediaResponse response = vodClient.DeleteMedia(request);
        log.info(JSON.toJSONString(response));
    }
}
